package com.philip.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * This is for diagnostics only.
 */
public abstract class CleanHsqldbLog { // NOPMD by r39 on 4/4/11 5:50 PM

    /** Connection SQL log. */
    private static final String PASSW = "CONNECT USER SA;\n";

    /** Erasure token 1. */
    private static final String REPLACE = "CONNECT USER SA;\nSET SCHEMA PUBLIC;\n";

    /** Erasure token 2. */
    private static final String REPLACE2 = "SET SCHEMA PUBLIC;\nCONNECT USER SA;\nDISCONNECT;\n";

    /**
     * Helper method for print out, to centralize PMD warning.
     *
     * @param message text to print to console.
     */
    private static void printScreen(final String message) {
        System.out.println(message); // NOPMD by r39 on 4/4/11 5:49 PM
    }

    /**
     * RTFC.
     *
     * @param param this is the entire text log.
     * @return the text cleansed with unwanted lines.
     */
    private static String removeUnwantedLines(final String param) {
        String string = param;
        while (string.indexOf(REPLACE + REPLACE) > -1) {
            string = string.replaceAll(REPLACE + REPLACE, REPLACE);
        }

        while (string.indexOf(REPLACE2 + REPLACE2) > -1) {
            string = string.replaceAll(REPLACE2 + REPLACE2, REPLACE2);
        }

        while (string.indexOf(PASSW) > -1) {
            string = string.replaceAll(PASSW, PASSW.substring(0, PASSW.length() - 2) + " PASSWORD '';\n");
        }

        string = removeToken(string, "DISCONNECT;\n");
        string = removeToken(string, "SET AUTOCOMMIT TRUE;\n");
        string = removeToken(string, "SET AUTOCOMMIT FALSE;\n");
        string = removeToken(string, "SET SCHEMA PUBLIC;\n");
        string = removeToken(string, "SET SCHEMA JRNL;\n");
        string = removeToken(string, "CONNECT USER SA PASSWORD '';\n");
        string = removeToken(string, "COMMIT;\n");

        string = replaceToken(string, "\n\n", "\n");
        return string;
    }

    /**
     * @param args unused for now. Log file is hard coded. TODO: May no longer need this tool.
     * @throws IOException standard IOException for main method.
     */
    public static void main(final String[] args) throws IOException {
        final BufferedReader buffReader = new BufferedReader(new FileReader(new File("U:\\journaldb.log")));

        final StringBuilder strBuilder = new StringBuilder();
        String previous = null;

        String sLines = removeComment(buffReader.readLine());
        while (sLines != null) {
            if (!sLines.equals(previous)) {
                strBuilder.append(sLines + ";\n");
                previous = sLines;
            }
            sLines = removeComment(buffReader.readLine());
        }

        final String string = removeUnwantedLines(strBuilder.toString());

        final Map<String, String> map = new LinkedHashMap<String, String>();
        final String[] sarr = string.split("\n");

        final List<String> soleInsert = new ArrayList<String>();
        for (int i = 0; i < sarr.length; i++) {
            if (sarr[i].startsWith("DELETE FROM")) {
                map.put(sarr[i], sarr[i + 1]);
                i++;
            } else {
                soleInsert.add(sarr[i]);
            }
        }

        printScreen("SET SCHEMA PUBLIC;");
        printScreen("CONNECT USER SA PASSWORD;");
        printScreen("SET SCHEMA JRNL;");
        printScreen("SET AUTOCOMMIT FALSE;");

        for (final String nextKey : map.keySet()) {
            printScreen(nextKey);
            printScreen(map.get(nextKey));
        }

        printScreen("--Sole Inserts");

        for (final String next : soleInsert) {
            printScreen(next);
        }
        printScreen("COMMIT;");
        buffReader.close();
    }

    /**
     * Removes all occurrences of 'token' in 'string'.
     *
     * @param string source text from where to remove the occurrence of token from.
     * @param token the text to remove.
     * @return text where all occurrences of token has been removed.
     */
    private static String removeToken(final String string, final String token) {
        String temp = string;
        while (temp.indexOf(token) > -1) {
            temp = temp.replaceAll(token, "");
        }
        return temp;
    }

    /**
     * RTFC.
     *
     * @param string input string.
     * @param token text to replace
     * @param replace replacement text
     * @return string where all occurrences of token has been replaced with 'replace'
     */
    private static String replaceToken(final String string, final String token, final String replace) {
        String temp = string;
        while (temp.indexOf(token) > -1) {
            temp = temp.replaceAll(token, replace);
        }
        return temp;
    }

    /**
     * RTFC.
     *
     * @param string input string.
     * @return removed comments from the passed string.
     */
    private static String removeComment(final String string) {
        String temp = string;
        if (temp != null && temp.indexOf("/*C") > -1) {
            temp = temp.substring(string.indexOf("*/") + 2);
        }
        return temp;
    }

}
